package com.example.administrator.demo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.administrator.demo.Bean.news;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by Administrator on 2016/4/25.
 */
public class Finance extends Fragment{
    private View finance;
    private SwipeRefreshLayout swipeRefreshLayout;
    private ListView listView;
    private ArrayList<HashMap<String, String>> news_list = new ArrayList<HashMap<String, String>>();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        finance = inflater.inflate(R.layout.finance, container, false);
        swipeRefreshLayout = (SwipeRefreshLayout)finance.findViewById(R.id.swipe_finance);
        listView = (ListView)finance.findViewById(R.id.listView_finance);

        swipeRefreshLayout.setOnRefreshListener(listener);
        listener.onRefresh();


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(finance.getContext(), detail.class);

                //获取选中项的HashMap对象
                HashMap<String, String> map = (HashMap<String, String>) listView.getItemAtPosition(position);
                intent.putExtra("ID", map.get("Item_ID"));

                startActivity(intent);
            }
        });

        return finance;
    }

    SwipeRefreshLayout.OnRefreshListener listener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            listView.setAdapter(null);
            add_data();
            swipeRefreshLayout.setRefreshing(false);
        }
    };

    private void add_data(){
        BmobQuery<news> query1 = new BmobQuery<news>();
        query1.addWhereEqualTo("category", "finance");

        BmobQuery<news> query2 = new BmobQuery<news>();
        query2.addWhereEqualTo("category", "money");

        List<BmobQuery<news>> queries = new ArrayList<BmobQuery<news>>();
        queries.add(query1);
        queries.add(query2);

        BmobQuery<news> mainQuery = new BmobQuery<news>();
        mainQuery.or(queries);

        //限制查询结果条数为50条，默认为10条
        mainQuery.setLimit(50);
        //按时间降序
        mainQuery.order("-time");
        //执行查询
        mainQuery.findObjects(finance.getContext(), new FindListener<news>() {
            @Override
            public void onSuccess(List<news> list) {
                for(news item : list){
                    //向动态数组里面加载查询到的数据
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("Item_ID", item.getID());
                    map.put("Item_title", item.getTitle());
                    map.put("Item_source", convert_source(item.getSource()));
                    map.put("Item_time", convert_time(item.getTime()));
                    map.put("Item_ID", item.getID());
                    news_list.add(map);
                }

                //生成适配器，数组==》ListItem
                SimpleAdapter schedule = new SimpleAdapter(finance.getContext(), //当前上下文
                        news_list,  //数据来源
                        R.layout.list_item,  //list_item的xml实现
                        new String[] {"Item_title", "Item_source" , "Item_time", "Item_ID"},  //动态数组与list_item对应的子项
                        new int[] {R.id.Item_title, R.id.Item_source, R.id.Item_time, R.id.Item_ID});  //list_item的xml文件中4个TextView ID

                //添加并显示
                listView.setAdapter(schedule);
            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }

    //转换来源信息
    private String convert_source(String source){
        String result = null;
        if(source.equals("sina")){
            result = "新浪";
        }
        else if (source.equals("tencent")){
            result = "腾讯";
        }
        else if (source.equals("netease")){
            result = "网易";
        }
        return result;
    }

    //转换时间信息，将12位的时间转化为mm-dd hh:mm
    private String convert_time(String time){
        String result;
        String month, day, hour, minute;
        month = time.substring(4,6);
        day = time.substring(6,8);
        hour = time.substring(8,10);
        minute = time.substring(10,12);

        result = month + "-" + day + " " + hour + ":" + minute;

        return result;
    }
}
